// pages/reading/reading.js
Page({
  data: {
    article: {
      ti: '',
      zz: '',
      tim: '',
      zw: ''
    }
  },

  onLoad: function(options) {
    // 从跳转链接获取文章ID
    const id = options.id;
    if (id) {
      this.getArticleDetail(id);
    }
  },

  getArticleDetail: function(id) {
    const db = wx.cloud.database({
      env: 'cloud1-3guckuse53913e2a'
    });
    const wz = db.collection('wz');
    
    wz.doc(id).get({
      success: (res) => {
        this.setData({
          article: {
            ti: res.data.ti || '无标题',
            zz: res.data.zz || '未知作者',
            tim: this.formatTime(res.data.tim) || '未知时间',
            zw: res.data.zw || '内容加载中...'
          }
        });
      },
      fail: (err) => {
        console.error('获取文章失败:', err);
        wx.showToast({
          title: '获取文章失败',
          icon: 'none'
        });
      }
    });
  },

  formatTime: function(timestamp) {
    if (!timestamp) return '';
    const date = new Date(timestamp);
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`;
  }
});